package com.huang.app.demo6.xpopup

import androidx.databinding.ViewDataBinding
import com.huang.app.demo6.vm.BasePopupViewModel
import com.huang.mvvm.control.ControlProvider
import com.huang.mvvm.control.IController
import org.greenrobot.eventbus.EventBus

abstract class BaseCenterPopupView<V : ViewDataBinding, VM : BasePopupViewModel<*>>(parent: IController) :
    BaseCenterMvvMPopupView<V, VM>
        (parent) {

    protected lateinit var controllerProvider: ControlProvider

    override fun onCreate() {
        super.onCreate()
        controllerProvider = ControlProvider(parent.get()!!)
        initParams()
        initViewModelParams()
        initView()
        viewModel.initEvent()
        viewModel.initData()
    }

    override fun onCreatedViewModel() {
        super.onCreatedViewModel()
        initDaggerInject()
    }

    override fun onBeforeObservable() {
        super.onBeforeObservable()
    }

    override fun onShow() {
        super.onShow()
        if (isRegisterEventBus()) EventBus.getDefault().register(this)
    }

    override fun onDismiss() {
        super.onDismiss()
        EventBus.getDefault().unregister(this)
    }

    override fun onDestroyController() {
        controllerProvider.get().onDestroy()
    }

    override fun showLoading(
        isCancelEnable: Boolean,
        isBackEnable: Boolean,
        onCanceledListener: (() -> Unit)?
    ) {
        controllerProvider.get().showLoading(this,isCancelEnable, isBackEnable, onCanceledListener)
    }

    override fun hideLoading() {
        controllerProvider.get().hideLoading()
    }

    override fun showMessage(text: String) {
        controllerProvider.get().showMessage(text)
    }

    /** 是否注册EventBus */
    open fun isRegisterEventBus(): Boolean {
        return false
    }

    /** 初始化页面参数  */
    open fun initParams() {

    }

    /** Dagger注入 */
    open fun initDaggerInject(){

    }

    /** 初始化或传递ViewModel的参数  */
    open fun initViewModelParams() {

    }

    /** 初始化视图  */
    open fun initView() {

    }

}